//
//  File Name:  ContentView.swift
//  Product Name:   Example
//  Created Date:   2024/5/12 09:20
//

import SwiftUI
import CalendarShared

struct ContentView: View {
    
    @State var content = ""
    @State var date = Date()
    
    var body: some View {
        VStack {
            Spacer()
            Text("月数据")
            HStack {
                Button(action: {
                    date = date.date(byAdding: .month, value: -1)
                    getMonth()
                }, label: {
                    Text("上月数据")
                })
                Button(action: {
                    date = Date()
                    getMonth()
                }, label: {
                    Text("当月数据")
                })
                Button(action: {
                    date = date.date(byAdding: .month, value: 1)
                    getMonth()
                }, label: {
                    Text("下月数据")
                })
            }
            Text("周数据")
            HStack {
                Button(action: {
                    date = date.date(byAdding: .weekday, value: -1)
                    getWeek()
                }, label: {
                    Text("上周数据")
                })
                Button(action: {
                    date = Date()
                    getWeek()
                }, label: {
                    Text("当周数据")
                })
                Button(action: {
                    date = date.date(byAdding: .weekday, value: 1)
                    getWeek()
                }, label: {
                    Text("下周数据")
                })
            }
            Text("日数据")
            HStack {
                Button(action: {
                    date = date.date(byAdding: .day, value: -1)
                    getWeek()
                }, label: {
                    Text("昨日数据")
                })
                Button(action: {
                    date = Date()
                    getWeek()
                }, label: {
                    Text("今日数据")
                })
                Button(action: {
                    date = date.date(byAdding: .day, value: 1)
                    getDay()
                }, label: {
                    Text("明日数据")
                })
            }
            TextEditor(text: $content)
        }
        .padding()
    }
    
    func getMonth() {
        let month = CalendarShared.shared.getMonth(date, firstWeek: .sun)
        let days = month.days.map { model in
            let fes = model.festivals.joined(separator: ",")
            let events = model.events.map{ $0.title }.joined(separator: ",")
            let reminders = model.reminders.map({ $0.title }).joined(separator: ",")
            let lunar = model.lunar
            let week = model.date.weekday == 0 ? "星期日" : "星期\(model.date.weekday)"
            return """
                        --------
            day: \(model.date.day)
            lunar: \(lunar)
            festivals: \(fes)
            week: \(week)
            reminders:\(reminders)
            events: \(events)
            mode: \(model.mode.rawValue)
            --------
            """
            "--------\n\(model.date.day)\n\(lunar)\n\(fes)\n\(week)\n\(reminders)\n\(events)\n\(model.mode.rawValue)\n--------"
        }.joined(separator: "\n")
        content = "\(month.year)-\(month.month)\n\(days)"
    }
    
    func getWeek() {
        let days = CalendarShared.shared.getWeek(date, firstWeek: .mon)
        let newDays = days.map { model in
            let fes = model.festivals.joined(separator: ",")
            let events = model.events.map{ $0.title }.joined(separator: ",")
            let reminders = model.reminders.map({ $0.title }).joined(separator: ",")
            let lunar = model.lunar
            let week = model.date.weekday == 0 ? "星期日" : "星期\(model.date.weekday)"
            return "--------\n\(model.date.day)\n\(lunar)\n\(fes)\n\(week)\n\(reminders)\n\(events)\n--------"
        }.joined(separator: "\n")
        content = newDays
    }
    
    func getDay() {
        let model = CalendarShared.shared.getDay(date)
        let fes = model.festivals.joined(separator: ",")
        let events = model.events.map{ $0.title }.joined(separator: ",")
        let reminders = model.reminders.map({ $0.title }).joined(separator: ",")
        let lunar = model.lunar
        content = "--------\(model.date.day)\n\(lunar)\n\(fes)\n\(events)\n\(reminders)\n--------"
    }
}

#Preview {
    ContentView()
}
